﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Repid.Entity.Basic;
using Repid.Core.Aop;

namespace Repid.MvcExtension
{
    public static class SessionFactory
    {
        const string SessionUserIDKey = "REPID-MANAGE-USERID";
        const string SessionUserNameKey = "REPID-MANAGE-USERNAME";
        const string SessionUserRoleKey = "REPID-MANAGE-USERROLE";
        const string SessionIsSuperKey = "REPID-MANAGE-ISSUPER";

        /// <summary>
        /// 用户编号
        /// </summary>
        public static string UserID
        {
            get
            {
                var obj = HttpContext.Current.Session[SessionUserIDKey];

                if (obj != null)
                {
                    return obj.ToString();
                }
                else { return string.Empty; }
            }
            set { HttpContext.Current.Session[SessionUserIDKey] = value; }
        }

        /// <summary>
        /// 用户账号
        /// </summary>
        public static string UserName
        {
            get
            {
                var obj = HttpContext.Current.Session[SessionUserNameKey];

                if (obj != null)
                {
                    return obj.ToString();
                }
                else { return string.Empty; }
            }
            set { HttpContext.Current.Session[SessionUserNameKey] = value; }
        }

        /// <summary>
        /// 用户角色
        /// </summary>
        public static string UserRole
        {
            get
            {
                var obj = HttpContext.Current.Session[SessionUserRoleKey];

                if (obj != null)
                {
                    return obj.ToString();
                }
                else { return string.Empty; }
            }
            set { HttpContext.Current.Session[SessionUserRoleKey] = value; }
        }

        /// <summary>
        /// 超级管理
        /// </summary>
        public static bool IsSuper
        {
            get
            {
                var obj = HttpContext.Current.Session[SessionIsSuperKey];

                if (obj != null)
                {
                    bool flag;

                    bool.TryParse(obj.ToString(), out flag);

                    return flag;
                }
                else { return false; }
            }
            set { HttpContext.Current.Session[SessionIsSuperKey] = value; }
        }

        /// <summary>
        /// 当前用户
        /// </summary>
        public static BasicUser CurrentUser
        {
            get { return ServiceFactory.BasicUser.Get(UserID); }
        }

        /// <summary>
        /// 用户验证
        /// </summary>
        public static bool ManageAuthenticated
        {
            get { return UserRole == "MANAGE" && !string.IsNullOrEmpty(UserID); }
        }

        /// <summary>
        /// 注销登录
        /// </summary>
        public static void LoginOut()
        {
            HttpContext.Current.Session[SessionUserIDKey] = null;
            HttpContext.Current.Session[SessionUserNameKey] = null;
            HttpContext.Current.Session[SessionUserRoleKey] = null;
            HttpContext.Current.Session[SessionIsSuperKey] = null;
        }
    }
}
